/**
 * @author rxliuli
 */
const app = new BasicTableVue({
    el: '#app',
    data() {
        return {
            api: moIllegalInfoApi,
            columns: [
                new TableColumn({width: 30, type: 'selection'}),
                new TableColumn({
                    prop: 'houseId',
                    title: '所在机房',
                    formatter: ({houseId}) => this.houseMap.get(houseId),
                }),
                new TableColumn({prop: 'ip', title: '应用 IP'}),
                new TableColumn({prop: 'accessDomain', title: '访问域名', customComponent: true}),
                new TableColumn({prop: 'accessUrl', title: '访问网址', customComponent: true}),
                new TableColumn({prop: 'accessCount', title: '访问次数'}),
                new TableColumn({prop: 'illegalInfo', title: '非法信息'}),
                new TableColumn({prop: 'dangerFactor', title: '危险系数'}),
                new TableColumn({
                    prop: 'dangerLevel',
                    title: '危险级别',
                    formatter: fieldFormatter.facade(fieldFormatter.moIllegalInfo.dangerLevelMap),
                }),
                new TableColumn({
                    prop: 'dealStatus',
                    title: '处置状态',
                    formatter: fieldFormatter.facade(fieldFormatter.moIllegalInfo.dealStatusMap),
                }),
                new TableColumn({
                    prop: 'disposerName',
                    title: '处置人',
                }),
                new TableColumn({
                    prop: 'dealTime',
                    title: '处置时间',
                    formatter: TableColumn.datetimeFormat,
                }),
                new TableColumn({
                    prop: 'insertTime',
                    title: '入库时间',
                    formatter: TableColumn.datetimeFormat,
                }),
                new TableColumn({prop: 'firstFoundTime', title: '首次发现时间', formatter: TableColumn.datetimeFormat}),
                new TableColumn({prop: 'lastFoundTime', title: '最近发现时间', formatter: TableColumn.datetimeFormat}),
            ],
            moIllegalInfoMap: new Map(),
            sysUsers: [],
            houses: [],
            houseMap: new Map(),
            dealPanelShow: false,
            dealPanelForm: {},
            dateNow: rx.dateFormat(new Date(), 'yyyy-MM-dd'),
        }
    },
    methods: {
        toggleDealPanelShow() {
            this.dealPanelShow = !this.dealPanelShow
        },
        createForm() {
            return new MoIllegalInfo({
                firstFoundTimeRange: [],
                lastFoundTimeRange: [],
                dealTimeRange: [],
                dealStatus: undefined,
                logOperation: undefined,
                dateNow: new Date(),
            })
        },
        selectAll() {
            this.$refs.tableRefs.toggleAllSelection();
        },
        async init() {
            this.initDealPanel()
            this.sysUsers = await sysUserApi.selectList();
            this.houses = await baseHouseInfoApi.selectList();
            this.houseMap = rx.arrayToMap(this.houses, ({id}) => id, ({houseName}) => houseName)
            const isFromWhitelist = forward.getData().isFromWhitelist
            if (isFromWhitelist) {
                const content = forward.getData().content
                this.page.current = 1
                this.page.size = 10
                this.form.illegalInfo = content
                await this.searchPage()
            }
        },
        async getPage(page, entity) {
            const result = await this.api.page(page, rx.excludeFields(rx.blankToNullField(this.form), ...Array.from(this.excludeFieldSet)))
            this.moIllegalInfoMap = rx.arrayToMap(result.records, ({id}) => id)
            return result
        },

        /**
         * 处置
         */
        async dealSelected() {
            const disposedIds = this.selectedList.filter((obj) => parseInt(obj.dealStatus) !== 1).map((obj) => obj.id)
            if (disposedIds.length !== 0) {
                rxPrompt.msg('只能勾选未处置的数据');
                return;
            }
            const ids = this.selectedList.filter((obj) => parseInt(obj.dealStatus) === 1).map((obj) => obj.id)
            const {
                dealStatus,
                logOperation,
                remark,
            } = this.dealPanelForm
            if (this.checkSelected() && await ajaxOption({
                res: this.api.dealBatch({
                    v1: dealStatus,
                    v2: logOperation,
                    v3: remark,
                    v4: ids.map(id => {
                        return this.moIllegalInfoMap.get(id);
                    }),
                }),
                danger: '处置失败',
                success: '处置成功',
            })) {
                await this.searchPage()
            }
        },
        /**
         * 取消处置
         */
        async dealCancelSelected() {
            const disposedIds = this.selectedList.filter((obj) => parseInt(obj.dealStatus) === 1).map((obj) => obj.id)
            if (disposedIds.length !== 0) {
                await rxPrompt.msg('只能勾选已处置的数据');
                return;
            }
            const ids = this.selectedList.filter((obj) => parseInt(obj.dealStatus) !== 1).map((obj) => obj.id)
            if (this.checkSelected() && await ajaxOption({
                res: this.api.dealCancelBatch(ids.map(id => {
                    return this.moIllegalInfoMap.get(id)
                })),
                danger: '取消处置错误',
                success: '取消处置成功',
            })) {
                await this.searchPage()
            }
        },
        /**
         * 设置信任
         */
        async setTrust() {
            const disposedIds = this.selectedList.filter((obj) => parseInt(obj.dealStatus) !== 1).map((obj) => obj.id)
            if (disposedIds.length !== 0) {
                await rxPrompt.msg('只能勾选未处置的数据');
                return;
            }
            if (this.checkSelected() && await ajaxOption({
                res: this.api.setTrust(this.selectedIdList.map(id => {
                    return this.moIllegalInfoMap.get(id)
                })),
                danger: '设置信任错误',
                success: '设置信任成功',
            })) {
                await this.searchPage()
            }
        },
        initDealPanel() {
            this.dealPanelForm = {
                dealStatus: 2,
                logOperation: undefined,
                remark: undefined,
            }
        },
    },
    computed: {
        fieldFormatter() {
            return fieldFormatter
        },
    },
    mounted() {
        this.form = this.createForm()
        this.searchPage()
        this.init()
    },
})